package com.sync.syncmasterdata.product.service.convert;

import com.sync.syncmasterdata.configuration.ConfInfo;
import com.sync.syncmasterdata.customer.service.http.HttpAPIServiceForCrmSync;
import com.sync.syncmasterdata.product.pojo.crm.InventoryForCrm;
import com.sync.syncmasterdata.product.pojo.erp.InventoryForU8;
import com.sync.syncmasterdata.product.pojo.oa.InventoryForOA;
import com.sync.syncmasterdata.product.repository.u8.InvExRepository;
import com.sync.syncmasterdata.product.repository.u8.InventoryRepository;
import com.sync.syncmasterdata.product.service.ConvertInterface;
import com.sync.syncmasterdata.util.NormalUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 * U8产品对象 转 crm产品对象
 */
@Service
@Slf4j
public class ProductU8toCRM implements ConvertInterface<InventoryForU8,InventoryForCrm> {

    @Autowired
    private ConfInfo confInfo;
    @Autowired
    private InvExRepository invExRepository;
    @Autowired
    private HttpAPIServiceForCrmSync httpAPIService;

    /**
     * 转换方法
     * @param u8
     * @return
     */
    @Override
    public InventoryForCrm convertTo(InventoryForU8 u8) {
        InventoryForCrm crm = new InventoryForCrm();
        crm.setNew_add_code(u8.getInvaddCode());
        /**
         * 可用量
         */
        crm.setNew_available(0.00);
        crm.setNew_end_date(null);
        /**
         * u8id
         */
        crm.setNew_code(u8.getCode());
        /**
         * 是否常规备货 因为U8没有该字段，统一默认不是
         */
        crm.setNew_isnormal(false);
        /**
         * 基本单位 需转换
         */
        String unitid=unitToCrmUnit(u8.getMain_measure());
        if (unitid != null) {
            crm.setNew_main_measure(String.format("/new_units(%s)", unitid));
            crm.setNew_sale_measure(String.format("/new_units(%s)", unitid));
        }else{
            crm.setNew_main_measure(null);
            crm.setNew_sale_measure(null);
        }
        crm.setNew_name(u8.getName());
        crm.setNew_saunit_rate(13.0f);
        crm.setNew_sort_code(u8.getSort_code());
        crm.setNew_specs(u8.getSpecs());
        crm.setNew_taxrate(13.00d);
        /**
         * 库位 中文
         */
        crm.setNew_warehouse_type(u8.getSelf_define4());
        /**
         * 品牌
         */
        crm.setNew_brand(u8.getSelf_define5());
        /**
         * 生产企业 oa生产企业文本
         */
        crm.setNew_enterprise(u8.getEnterprise());
        crm.setNew_domestic_or_imported(u8.getBImport());
        Map<String, String> _exMap = getExMap(u8.getCode());
        if (!_exMap.isEmpty()) {
            crm.setNew_business_type(_exMap.getOrDefault("cidefine2", ""));
            crm.setNew_line(_exMap.getOrDefault("cidefine3", ""));
            crm.setNew_group(_exMap.getOrDefault("cidefine4", ""));
            crm.setNew_project(_exMap.getOrDefault("cidefine5", ""));
        }

        return crm;
    }

    /**
     * 获取u8扩展字段
     */
    private Map<String,String> getExMap(String code){
        Map<String,String> map= invExRepository.getProductExByCode(code);
        return map;
    }


    /**
     * 转换单位 to crmGUID
     */
    private String unitToCrmUnit(String unitCode) {
        try {
            //增加编码
            String path = NormalUtil.encodeUrl(String.format("%s%s", confInfo.getCrmurl(), "new_units?$filter=new_code eq '" + unitCode + "'"));

            String responseStr = httpAPIService.doGetCrm(path);
            JSONObject jsonObject = new JSONObject(responseStr);
            log.info("CRM单位换算:" + path);
            JSONArray jsonArray = jsonObject.getJSONArray("value");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = (JSONObject) jsonArray.get(i);
                return json.get("new_unitid").toString();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return null;
    }

    /**
     * 常规备货的转换
     */
    private boolean convertChangguibh(Integer i){
        try {
            if (i == 0) {
                return false;
            }else{
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }
}
